package com.ayoka.parenting.adapter;

import android.text.TextUtils;
import android.widget.ImageView;

import com.ayoka.parenting.R;
import com.jincp.common.brvah.BaseQuickAdapter;
import com.jincp.common.brvah.BaseViewHolder;
import com.jincp.image.load.ImageLoader;

import java.util.List;

/**
 * Created by jinchunping on 2018/4/11.
 */

public class PickPictureAdapter extends BaseQuickAdapter<String, BaseViewHolder> {

    public PickPictureAdapter() {
        super(R.layout.item_pick_picture, null);
    }

    @Override
    protected void convert(final BaseViewHolder holder, String item) {
        if (TextUtils.isEmpty(item)) {
            holder.setVisible(R.id.rl_has_picture, false)
                    .setVisible(R.id.rl_add_picture, true);
            holder.addOnClickListener(R.id.rl_add_picture);
        } else {
            holder.setVisible(R.id.rl_has_picture, true)
                    .setVisible(R.id.rl_add_picture, false);
            ImageLoader.getInstance().displayImage(item, (ImageView) holder.getView(R.id.iv_picture));
            holder.addOnClickListener(R.id.iv_delete_picture)
                    .addOnClickListener(R.id.rl_has_picture);
        }
    }

    public void deletePicture(int position) {
        try {
            remove(position);
            if (!getData().contains("") && getItemCount() < 9) {
                getData().add("");
            }
            notifyDataSetChanged();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public List<String> getPictureUrlList() {
        List<String> result = getData();
        if (result.contains("")) {
            result.remove("");
        }
        return result;
    }

    public String getPictureUrlString() {
        StringBuilder stringBuilder = new StringBuilder();
        for (String filePath : getPictureUrlList()) {
            if (TextUtils.isEmpty(filePath)) continue;
            if (stringBuilder.length() != 0) {
                stringBuilder.append(",");
            }
            stringBuilder.append(filePath);
        }
        return stringBuilder.toString();
    }
}
